/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BAMagazineCell.m
//  redia-bapps-ios
//
//  Created by Brian Vestergaard Danielsen on 05/11/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BAMagazineCell.h"
#import "BALightFrameButton.h"
#import "BAAddReservationViewController.h"
#import "BAPatronSettings.h"

@interface BAMagazineCell()

@property (weak, nonatomic) IBOutlet BALightFrameButton* reservationButton;
@property (weak, nonatomic) IBOutlet UILabel* magazineTitle;
@property (weak, nonatomic) BAHoldingContainer* holdingContainer;


@end

@implementation BAMagazineCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)awakeFromNib
{
    BA_LABEL_SET_FONT_LIGHT(self.reservationButton.titleLabel);
    BA_LABEL_SET_FONT_LIGHT(self.magazineTitle);

}

- (void)setHoldingContainer:(BAHoldingContainer*)container withVolumeTitle:(NSString*)volumeTitle
{
    _holdingContainer = container;
    

    if (RD_IS_IPAD) {
        NSString* newVolumeTitle = [NSString stringWithFormat:@"%@, %@", volumeTitle, container.title];
        [self.magazineTitle setText:newVolumeTitle];
    } else {
        [self.magazineTitle setText:container.title];
    }
    
    if ([[BAPatronSettings instance] isUserAllowedLibrarianAccessForCurrentLibrary]) {
        self.reservationButton.hidden=NO;
        [self.reservationButton setTitle:@"STATUS" forState:UIControlStateNormal];

    } else {
        //not librarian mode
        if (self.holdingContainer.availability!= nil && self.holdingContainer.availability.isReservable && [self.holdingContainer.availability.reservationId length]>0) {
            self.reservationButton.hidden = NO;
        } else {
            self.reservationButton.hidden = YES;
        }
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)reserveButtonClicked:(id)sender {
    
    if ([[BAPatronSettings instance] isUserAllowedLibrarianAccessForCurrentLibrary]) {
        NSDictionary* userinfo = nil;
        if (self.holdingContainer.availability) {
            userinfo = @{ @"availability" : self.holdingContainer.availability};
        }

        [[NSNotificationCenter defaultCenter] postNotificationName:LB_SHOW_HOLDING_HISTORY_FOR_MAGAZINE_ITEM
                                                            object:self
                                                          userInfo:userinfo
         ];
    
    } else {
        if (self.holdingContainer.availability!= nil && self.holdingContainer.availability.isReservable && [self.holdingContainer.availability.reservationId length]>0) {
            
            //when reservation is completed, a notification will be sent
            [BAAddReservationViewController createAddReservationDialogWithReservationId:self.holdingContainer.availability.reservationId title:self.holdingContainer.title];
        }
    }
}


@end
